#                                                                    -*-perl-*-

$description = "Test the --warn option.";

my %warn_test = (
    '--warn' => '', '--warn=warn' => '', '--warn=error --warn=warn' => '',
    '--warn --warn=error' => '=error',
    '--warn=ignore --warn=error --warn=ignore --warn=invalid-ref,invalid-var,undefined-var' => '=ignore,invalid-ref,invalid-var,undefined-var',
    '--warn=invalid-ref:ignore --warn=error --warn=invalid-var:warn,,,,,undefined-var:error,,,,,' => '=error,invalid-ref:ignore,invalid-var,undefined-var:error'
);

# Verify the deprecated --warn-undefined-variables option
run_make_test(q!
$(info MF=$(MAKEFLAGS))
all:; @#HELPER# env MAKEFLAGS
!,
              '--warn-undefined-variables', "MF= --warn=undefined-var\nMAKEFLAGS= --warn=undefined-var");

# sv 64115.
# Verify that -R along with --warn-undefined-variables do not warn about
# internal variables, such as GNUMAKEFLAGS.

run_make_test(q!
$(info MF=$(MAKEFLAGS))
all:; @#HELPER# env MAKEFLAGS
!,
              '-R --warn-undefined-variables', "MF=rR --warn=undefined-var\nMAKEFLAGS=rR --warn=undefined-var");


# Verify parsing of --warn in various forms.

while (my ($f, $r) = each %warn_test) {
    run_make_test(undef, $f, "MF= --warn$r\nMAKEFLAGS= --warn$r");
}

# Verify that values set in MAKEFLAGS take effect

while (my ($f, $r) = each %warn_test) {
    run_make_test(qq!
MAKEFLAGS += $f
\$(info MF=\$(MAKEFLAGS))
all:; \@#HELPER# env MAKEFLAGS
!,
                  '', "MF= --warn$r\nMAKEFLAGS= --warn$r");
}

# Verify that make's special variables don't warn even if they're not set
run_make_test(q!
vars := $(.VARIABLES) $(MAKECMDGOALS) $(MAKE_RESTARTS) $(CURDIR)
vars += $(GNUMAKEFLAGS) $(MAKEFLAGS) $(MFLAGS) $(MAKE_COMMAND) $(MAKE)
vars += $(MAKEFILE_LIST) $(MAKEOVERRIDES) $(-*-command-variables-*-)
vars += $(.RECIPEPREFIX) $(.LOADED) $(.FEATURES)
vars += $(SHELL) $(.SHELLFLAGS) $(MAKE_TERMOUT) $(MAKE_TERMERR)
vars += $(.DEFAULT) $(.DEFAULT_GOAL) $(-*-eval-flags-*-) $(SUFFIXES)
vars += $(VPATH) $(GPATH)
all:;
!,
              '--warn=undefined-var', "#MAKE#: 'all' is up to date.");

# sv 63609.
# Test for buffer overrun in warn_undefined.
run_make_test(q!
all:;
X := $(averyveryveryloooooooooooooooooooooooooooongvariablename)
!,
              '--warn=undefined-var',
              "#MAKEFILE#:3: warning: reference to undefined variable 'averyveryveryloooooooooooooooooooooooooooongvariablename'
#MAKE#: 'all' is up to date.\n"
);

# Check undefined variable warnings

# With no options or with ignore, nothing should happen
run_make_test('
EMPTY =
EREF = $(EMPTY)
UREF = $(UNDEFINED)

SEREF := $(EREF)
SUREF := $(UREF)

all: ; @echo ref $(EREF) $(UREF)',
              '', 'ref');

run_make_test(undef, '--warn=undefined-var:ignore', 'ref');

# Check warnings
run_make_test(undef, '--warn=undefined-var',
              "#MAKEFILE#:7: warning: reference to undefined variable 'UNDEFINED'
#MAKEFILE#:9: warning: reference to undefined variable 'UNDEFINED'
ref");

# Check and errors
run_make_test(undef, '--warn=undefined-var:error',
              "#MAKEFILE#:7: *** reference to undefined variable 'UNDEFINED'.  Stop.", 512);

# Check invalid variable reference warnings

# With no options we still check for invalid references
run_make_test('
IREF = $(bad variable)
SIREF := $(IREF)

define nl


endef

all: ; @echo ref $(also$(nl)bad) $(IREF) $(SIREF)',
              '', "#MAKEFILE#:2: warning: invalid variable reference 'bad variable'
#MAKEFILE#:10: warning: invalid variable reference 'also\nbad'
#MAKEFILE#:2: warning: invalid variable reference 'bad variable'
ref");

run_make_test(undef, '--warn=ignore', 'ref');

run_make_test(undef, '--warn=invalid-ref:ignore', 'ref');

# Check and errors
run_make_test(undef, '--warn=invalid-ref:error',
              "#MAKEFILE#:2: *** invalid variable reference 'bad variable'.  Stop.", 512);

# Check invalid variable name warnings

# With no options we still check for invalid references
run_make_test('
EMPTY =
SPACE = $(EMPTY) $(EMPTY)
BAD$(SPACE)VAR = foo

define nl


endef

NL$(nl)VAR = bar

define BAD$(SPACE)DEF :=
foo
endef

define NL$(nl)DEF :=
foo
endef

all: ; @echo ref',
              '', "#MAKEFILE#:4: warning: invalid variable name 'BAD VAR'
#MAKEFILE#:11: warning: invalid variable name 'NL\nVAR'
#MAKEFILE#:13: warning: invalid variable name 'BAD DEF'
#MAKEFILE#:17: warning: invalid variable name 'NL\nDEF'
ref");

run_make_test(undef, '--warn=ignore', 'ref');

run_make_test(undef, '--warn=invalid-var:ignore', 'ref');

# Check errors
run_make_test(undef, '--warn=invalid-var:error',
              "#MAKEFILE#:4: *** invalid variable name 'BAD VAR'.  Stop.", 512);

# Make sure unknown warnings and actions fail when given on the command line.

run_make_test(undef, '--warn=no-such-warn',
    "#MAKE#: *** unknown warning 'no-such-warn'.  Stop.", 512);

run_make_test(undef, '--warn=invalid-var:no-such-action',
    "#MAKE#: *** unknown warning action 'no-such-action'.  Stop.", 512);

# sv 65739. Circular dependency.
run_make_test(q!
hello: hello; @:
!,
              '', "#MAKE#: circular hello <- hello dependency dropped\n");

run_make_test(undef, '--warn=error', "#MAKE#: *** circular hello <- hello dependency detected.  Stop.\n", 512);
run_make_test(undef, '--warn=circular-dep:error', "#MAKE#: *** circular hello <- hello dependency detected.  Stop.\n", 512);
run_make_test(undef, '--warn=warn', "#MAKE#: circular hello <- hello dependency dropped\n");
run_make_test(undef, '--warn=circular-dep:warn', "#MAKE#: circular hello <- hello dependency dropped\n");
run_make_test(undef, '--warn=ignore', '');
run_make_test(undef, '--warn=circular-dep:ignore', '');

1;
